
// === uuMeta.event.resize ===
// depend: uuMeta, uuMeta.event.base
/*
uuMeta.event.resize(callback, type = false) - return atom
 */
// |          | innerWidth | clientWidth |
// |IE8       | x          | o           |
// |Firefox2+ | o          | o           |
// |Opera9.2+ | o          | o           |
// |Safari4   | o          | o           |
// |Chrome4   | o          | o           |
(function uuMetaEventResizeScope() {
var _mm = uuMeta,
    _root = _mm.ua.ie ? (_mm.ua.quirks ? document.body
                                       : document.documentElement)
                      : window,
    _db1 = { delay:  40, lock: 0, kill: 0, callback: [] },
    _db2 = { delay: 100, lock: 0, kill: 0, callback: [] },
    _INNER_WIDTH  = _mm.ua.ie ? "clientWidth"  : "innerWidth",
    _INNER_HEIGHT = _mm.ua.ie ? "clientHeight" : "innerHeight";

// event handler
function type1(callback) { // @param Function/0: callback function or zero
                           // @return Number: callback function atom
  if (!callback) {
    _db1.kill = 1;
    return 0;
  }
  !_db1.callback.length && _mm.event.bind(window, "resize", ontype1); // init
  _db1.callback.push(callback); // regist
  return _db1.length - 1; // return atom
}

function ontype1() {
  if (_db1.kill) {
    _mm.event.unbind(window, "resize", ontype1);
    _db1.kill = _db1.lock = 0;
    _db1.callback = []; // clear
    return;
  }
  if (!_db1.lock++) {
    setTimeout(function() {
      var i = 0, iz = _db1.callback.length;

      for (; i < iz; ++i) {
        (i in _db1.callback) && _db1.callback[i]();
      }
      setTimeout(delayUnlock1, 0);
    }, _db1.delay);
  }
}

// resize agent
function type2(callback) { // @param Function/0: callback function or zero
                           // @return Number: callback function atom
  if (!callback) {
    _db2.kill = 1;
    return 0;
  }
  if (!_db2.callback.length) {
    _db2.innerSize = { w: _root[_INNER_WIDTH],
                       h: _root[_INNER_HEIGHT] };
  }
  _db2.callback.push(callback); // regist

  function loop() {
    if (_db2.kill) {
      _db2.kill = _db2.lock = 0;
      _db2.callback = []; // clear
      return;
    }
    if (!_db2.lock++) {
      var i, iz,
          dim = { w: _root[_INNER_WIDTH],
                  h: _root[_INNER_HEIGHT] };

      if (_db2.innerSize.w !== dim.w ||
          _db2.innerSize.h !== dim.h) {
        // resized
        _db2.innerSize = dim;

        for (i = 0, iz = _db2.callback.length; i < iz; ++i) {
          (i in _db2.callback) && _db2.callback[i]();
        }
      }
      setTimeout(delayUnlock2, 0);
    }
    setTimeout(loop, _db2.delay);
  }
  setTimeout(loop, _db2.delay);

  return _db2.length - 1; // return atom
}

// inner -
function delayUnlock1() {
  _db1.lock = 0;
}

// inner -
function delayUnlock2() {
  _db2.lock = 0;
}

// uuMeta.event.resize
function resize(callback, // @param Function/0: callback function or zero
                agent) {  // @param Boolean(= false): use resize agent
  return agent ? type2(callback)  // use resize agent
               : type1(callback); // use event handler
}

// --- initialize ---

// --- export ---
_mm.event.resize = resize;

})(); // uuMeta.event.resize scope

